<?php
require_once '../conn.php';
require_once '../libs/global.cls';

$gname = $_POST['txtgroup'];
$sid = $_POST["txtsid"];
$cid = $_POST['txtcid'];
$ref = $_POST['txtacg'];    //ref=acg, 则页面是从添加联系人跳转过来的

// 分组为空
if(empty ($gname)){
    $error_type = 401;
    include_once '../error/group_error.php';
    exit;
}

// 判断数据库该身份对应的分组是否存在
$sql = "select gname from cc_group where sid='". $sid ."'";
$res = $ms_cc_pdo->select($sql);
$row = $res['result'];
foreach ($row as $key=>$item){
    if($item['gname'] == $gname){
        $error_type = 303;
        $cid = $cid;
        $sid = $sid;
        $ref = $ref;
        include_once '../error/group_error.php';
        exit;
    }
}

// 验证通过，添加分组
$params = array();
$params[':sid'] = (int)$sid;
$params[':gname'] = $gname;
$params[':ctime'] =  time();

//添加到数据库
$sql_insert = sprintf( "INSERT INTO `cc_group` (`gname`,  `sid`, `ctime`) VALUES ( :gname, :sid, :ctime )" );

$res_insert = $ms_cc_pdo->insert($sql_insert,$params);
if(!$res_insert['errorcode']){
    if($ref=='acg'){
        redirect("../contact/contact_add_group.php?cid=$cid&sid=$sid");
    }
    // todo: 其它情况跳转
}

?>
